package com.rop.znyx.home.service.impl;

import com.rop.znyx.activity.ActivityFeignClient;
import com.rop.znyx.activity.SecKillFeignClient;
import com.rop.znyx.home.service.ItemService;
import com.rop.znyx.product.ProductFeignClient;
import com.rop.znyx.search.SearchFeignClient;
import com.rop.znyx.vo.product.SkuInfoVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created with IntelliJ IDEA
 * User: Rop
 * Date: 2023/7/21
 * Time: 13:35
 * Description:
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class ItemServiceImpl implements ItemService {


    private ProductFeignClient productFeignClient;

    private ActivityFeignClient activityFeignClient;

    private SecKillFeignClient secKillFeignClient;

    private SearchFeignClient searchFeignClient;

    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 获取sku详细信息
     */
    @Override
    public Map<String, Object> item(Long skuId, Long userId) {
        Map<String, Object> result = new HashMap<>();

        // 通过skuId 查询skuInfo
        CompletableFuture<Void> skuInfoCompletableFuture = CompletableFuture.runAsync(() -> {
            //sku基本信息
            SkuInfoVo skuInfoVo = productFeignClient.getSkuInfoVo(skuId).getData();
            result.put("skuInfoVo", skuInfoVo);
        }, threadPoolExecutor);

        //TODO 如果商品是秒杀商品，获取秒杀信息
        CompletableFuture<Void> seckillSkuCompletableFuture = CompletableFuture.runAsync(() -> {
        }, threadPoolExecutor);

        CompletableFuture<Void> activityCompletableFuture = CompletableFuture.runAsync(() -> {
            //sku对应的促销与优惠券信息
            Map<String, Object> activityAndCouponMap = activityFeignClient.findActivityAndCoupon(skuId, userId).getData();
            result.putAll(activityAndCouponMap);
        }, threadPoolExecutor);

        CompletableFuture<Void> hotCompletableFuture = CompletableFuture.runAsync(() -> {
            searchFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        //  任务组合：
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                seckillSkuCompletableFuture,
                activityCompletableFuture,
                hotCompletableFuture
        ).join();

        return result;
    }
}
